﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using Cosmetic.DataEntity;

namespace Cosmetic.Logic
{
    public class FocusManager : LogicBase
    {
        public IEnumerable GetFocusProduct()
        {
            var result = from theFocus in m_CosmeticDC.FocusProducts
                         join theProduct in m_CosmeticDC.ProductInfos
                         on theFocus.ProductId equals theProduct.ProductId
                         orderby theFocus.Sequence ascending
                         select new
                         {
                             Id = theFocus.Id,
                             ProductId = theProduct.ProductId,
                             Name = theProduct.Name,
                             Views = theProduct.Views,
                             PicUrl = theProduct.PictureUrl,
                             Sequence = theFocus.Sequence,
                             Price = theProduct.网络零售价
                         };
            return result;
        }

        public void UpdateFocusProductSequence(int Id, int Sequence)
        {
            var result = (from theFocus in m_CosmeticDC.FocusProducts
                          where theFocus.Id == Id
                          select theFocus).Single();
            result.Sequence = Sequence;
            m_CosmeticDC.SubmitChanges();
        }

        public void DeleteFocusProduct(int Id)
        {
            var result = (from theFocus in m_CosmeticDC.FocusProducts
                          where theFocus.Id == Id
                          select theFocus).Single();
            m_CosmeticDC.FocusProducts.DeleteOnSubmit(result);
            m_CosmeticDC.SubmitChanges();
        }

        public void InsertFocusProduct(string ProductId)
        {
            FocusProduct theFocus = new FocusProduct();
            theFocus.CreateTime = DateTime.Now;
            theFocus.ProductId = new Guid(ProductId);
            var result = from theMax in m_CosmeticDC.FocusProducts orderby theMax.Sequence descending select theMax;
            if (result.Count()==0)
            {
                theFocus.Sequence = 0;
            }
            else
            {
                theFocus.Sequence = (from theMax in m_CosmeticDC.FocusProducts orderby theMax.Sequence descending select theMax).First().Sequence + 1;
            }

            m_CosmeticDC.FocusProducts.InsertOnSubmit(theFocus);
            m_CosmeticDC.SubmitChanges();
        }



    }
}
